﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Common;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RMS.Common;
using RMS.DB;

namespace RMS
{
    public partial class frmListDraftInvoices : ICommonForm
    {
        private readonly InvoicesMng _invoices = new InvoicesMng();
        private readonly SoldItemsMng _soldItemsMng = new SoldItemsMng();
        public frmListDraftInvoices()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void frmListDraftInvoices_Load(object sender, EventArgs e)
        {
            dataGridView1.AutoGenerateColumns = false;
            BindData();
        }
        private void BindData()
        {
            var src = _invoices.GetDraftInvoices();
            dataGridView1.DataSource = src;
            dataGridView1.ClearSelection();
            txtSearch.Focus();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 1)
            {
                btnDel.Enabled = true;
                foreach (DataGridViewRow row in dataGridView1.Rows)
                {
                    if (row.Cells[1].Selected)
                    {
                        row.Selected = true;
                        var orderCode = Convert.ToString(row.Cells[5].Value);
                        if (!string.IsNullOrEmpty(orderCode))
                        {
                            var draftItem = _invoices.GetItemById(orderCode);
                            if (draftItem != null)
                            {
                                if (draftItem.ProductType.Equals(Constants.IsInventoryItem))
                                {
                                    var frmInv = new frmSellInventory(draftItem) { WindowState = FormWindowState.Normal, StartPosition = FormStartPosition.CenterScreen };
                                    frmInv.MdiParent = MdiParent = frmMain.ActiveForm;
                                    frmInv.Show();
                                }
                            }
                            this.Close();
                        }
                    }
                }
            }
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            bool flag = false;
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                if (Convert.ToBoolean(row.Cells[0].Value))
                {
                    flag = true;
                }
            }
            if (flag)
            {
                if (MessageBox.Show(Constants.Msg.DeleteInvoice, "Chú ý", MessageBoxButtons.YesNo) != DialogResult.Yes)
                    return;
                foreach (DataGridViewRow row in dataGridView1.Rows)
                {
                    if (Convert.ToBoolean(row.Cells[0].Value))
                    {
                        var orderCode = Convert.ToString(row.Cells[5].Value);
                        var listitem = _soldItemsMng.GetListItems(Convert.ToString(row.Cells[5].Value));
                        foreach (var item in listitem)
                        {
                            _soldItemsMng.DeleteItem(item.ProductID, orderCode);
                        }
                        _invoices.DeleteItem(Convert.ToString(row.Cells[5].Value));
                    }
                }
                BindData();
            }
            else
            {
                MessageBox.Show(Constants.Msg.ChooseOneInvoice, "Chú ý");
                return;
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            var src = _invoices.GetDraftInvoices(txtSearch.Text.Trim());
            dataGridView1.DataSource = src;
            dataGridView1.ClearSelection();
            btnDel.Enabled = false;
        }
    }
}
